<?php
/*
 * CRMWebUser File
 *
 * @link http://riedel-st.de
 * @author Stefan Riedel <sr@riedel-st.de>
*/

/**
 * Description of CRMWebUser
 *
 * @author Stefan Riedel
 */
class CRMWebUser extends CWebUser {

    protected $_superUser;

    /**
     * 
     * @var Users
     */
    protected $_model;
    
    public function getPhone_Id() {
    	$user = $this->_loadUser();
    	return (int)$user->users_phone_id;	
    }
    
    public function setPhone_Id($value) {
    	$user = $this->_loadUser();
    	$user->users_phone_id = $value;
    	return $user->save();
    }
    
    public function getPhone_Number() {
    	$user = $this->_loadUser();
    	if(0 === (int)$user->users_phone_id) {
    		$number = null;
    	}
    	else {
    		$phone = Phones::model()->findByPk($user->users_phone_id);
    		if($phone) {
    			$number = $phone->phones_number;
    		}
    		else {
    			$number = null;
    		}
    	}
    	return $number;
    }
    
    public function init() {
        parent::init();
        $this->_superUser = Yii::app()->dbConfig->superUser;
    }

    public function checkAccess($operation,$params=array(),$allowCaching=true) {
        if(parent::checkAccess($this->_superUser)) {
            return true;
        }
        return parent::checkAccess($operation, $params, $allowCaching);        
    }
    
    protected function _loadUser() {
    	if(!$this->_model) {
    		$this->_model = Users::model()->findByPk(Yii::app()->user->id);	
    	}
    	return $this->_model;
    }
    
	public function __toString() {
		$user = $this->_loadUser();
		return $user->users_firstname . ' ' . $user->users_lastname;
	}    
    
}
?>
